import QtQuick
import QtQuick.Layouts
import QtQuick.Controls

import Fk
import Fk.Components.LunarLTK
import Fk.Pages.LunarLTK
import Fk.Components.Common

ColumnLayout {
  id: root
  anchors.fill: parent
  property var extra_data: ({ name: "", data: [] })
  signal finish()

  BigGlowText {
    Layout.fillWidth: true
    Layout.preferredHeight: childrenRect.height + 4

    //text: Lua.tr(extra_data.name)
    text: Lua.tr("#" + extra_data.name + "_tip")
  }

  ListView {
    id: body
    Layout.fillWidth: true
    Layout.fillHeight: true

    clip: true
    spacing: 20

    // 直接使用数组作为模型
    model: extra_data.data

    delegate: Rectangle {
      width: body.width
      height: 210
      color: "transparent"
      border.color: "#E4D5A0"
      border.width: 2
      radius: 8
      
      property var playerId: modelData[0] // 第一个元素是玩家ID
      property var skills: modelData.slice(1) // 其余元素是技能列表，每个技能是[技能名, bool]格式

      Row {
        anchors.fill: parent
        anchors.margins: 10
        spacing: 15

        // 玩家信息部分
        Item {
          id: playerArea
          width: 130
          height: parent.height
          
          Photo {
            id: playerPhoto
            
            // 通过Lua获取玩家信息
            property var playerInfo: {
              try {
                // 将玩家ID转换为整数
                var id = parseInt(playerId);
                return Lua.evaluate(
                  `(function()
                    local player = ClientInstance:getPlayerById(${id})
                    if player then
                      return {
                        id = player.id,
                        general = player.general or "",
                        deputyGeneral = player.deputyGeneral or "",
                        screenName = player.player:getScreenName() or "",
                        kingdom = player.kingdom or "",
                        seat = player.seat or 0,
                        role = 'hidden',
                      }
                    else
                      return { 
                        id = ${id}, 
                        general = "", 
                        screenName = "Unknown Player",
                        kingdom = "",
                        seat = 0,
                        role = 'hidden'
                      }
                    end
                  end)()`
                );
              } catch (e) {
                console.log("Error getting player info:", e);
                return { 
                  id: playerId, 
                  general: "", 
                  screenName: "Error Loading Player",
                  kingdom: "",
                  seat: 0,
                  role: 'hidden'
                };
              }
            }



            playerid: playerInfo.id
            general: playerInfo.general
            deputyGeneral: playerInfo.deputyGeneral
            role: playerInfo.role
            state: "candidate"
            screenName: playerInfo.screenName
            kingdom: playerInfo.kingdom
            seatNumber: playerInfo.seat

            Component.onCompleted: {
              if (this.visibleChildren && this.visibleChildren.length > 12) {
                this.visibleChildren[12].visible = false;
              }
            }
          }
        }

        // 技能按钮部分 - 根据技能数量动态调整布局
        Item {
          width: parent.width - playerArea.width - 10 // 减去玩家信息部分宽度和间距
          height: parent.height
          
          Grid {
            anchors.verticalCenter: parent.verticalCenter
            anchors.left: parent.left
            rows: Math.ceil(skills.length / 8)
            columns: 8
            rowSpacing: 5
            columnSpacing: 5
            Repeater {
              model: skills

              Item {  // 需要填入固定数值
                width: 88
                height: 36
                
                SkillButton {
                  anchors.fill: parent
                  skill: Backend.translate(modelData[0]) // 第一个元素是技能名
                  type: "active"
                  enabled: !modelData[1] // 第二个元素为true时禁用按钮
                  orig: modelData[0]
                }
              }
            }
          }
        }
      }
    }
  }

  onExtra_dataChanged: {
    // 如果传入的是字符串，转换为数组格式
    if (typeof(extra_data.data) == "string") {
      extra_data.data = [[extra_data.data]];
    }
    // 如果传入的是单个数组，包装成二维数组
    else if (Array.isArray(extra_data.data) && extra_data.data.length > 0 && typeof(extra_data.data[0]) != "object") {
      extra_data.data = [extra_data.data];
    }
  }
}